<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/commons/global.jsp"%>

<table id="tagAudioList" style="height: 100%; width: 100%;">

</table>

<div id="tagAudioListToolBars" style="height: auto">
	<a href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-remove',plain:true"
		onclick="tagAudioList_remove()">删除</a>| <a href="javascript:void(0)"
		class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true"
		onclick="tagAudioList_append()">添加</a>|<a href="javascript:void(0)"
		class="easyui-linkbutton"
		data-options="iconCls:'icon-edit',plain:true"
		onclick="tagAudioList_edit()">编辑</a> | <a href="javascript:void(0)"
		class="easyui-linkbutton"
		data-options="iconCls:'icon-undo',plain:true"
		onclick="tagAudioList_undo()">取消</a>| <a href="javascript:void(0)"
		class="easyui-linkbutton"
		data-options="iconCls:'icon-save',plain:true"
		onclick="tagAudioList_accept()">保存</a> |<input
		id="tagAudioList_AudioStatusQue" style="width: 100px;" /> |<input
		id="tagAudioList_AudioTitleQue" style="width: 200px;" /> |<input
		id="tagAudioList_TagNameQue" style="width: 200px;" />
</div>

<script type="text/javascript">
	function tagAudioListDatagridReload() {
		try {
			var queryParams = {
				like_title : tagAudioList_AudioTitleQue.textbox('getValue'),
				like_tag_name : tagAudioList_TagNameQue.textbox('getValue'),
				itemType : 2,
				status : tagAudioList_AudioStatusQue.combobox('getValue')
			};
			tagAudioListDatagrid.datagrid("options").queryParams = queryParams;
			tagAudioListDatagrid.datagrid("reload");
		} catch (e) {
		}
	}
</script>

<script type="text/javascript">
	var tagAudioList_AudioStatusQue = undefined;
	var tagAudioList_TagNameQue = undefined;
	var tagAudioList_AudioTitleQue = undefined;
	var tagAudioSelectIndex = undefined;
	var tagAudioSelectRow = undefined;
	var tagAudioEditIndex = undefined;
	var tagAudioEditRow = undefined;
	var tagAudioListDatagrid = undefined;
	$(function() {
		tagAudioList_AudioStatusQue = $('#tagAudioList_AudioStatusQue')
				.combobox({
					data : [ {
						"statusCode" : 0,
						"statusMsg" : "全部状态",
						"selected" : true
					}, {
						"statusCode" : 1,
						"statusMsg" : "已发布"
					}, {
						"statusCode" : 2,
						"statusMsg" : "已下线"
					}, {
						"statusCode" : 3,
						"statusMsg" : "待发布"
					} ],
					valueField : 'statusCode',
					textField : 'statusMsg',
					onChange : function(newValue, oldValue) {
						tagAudioListDatagridReload();
					}
				});

		tagAudioList_TagNameQue = $('#tagAudioList_TagNameQue').textbox({
			buttonText : '标签名搜索',
			buttonIcon : 'icon-search',
			buttonAlign : 'right'
		});

		tagAudioList_TagNameQue.textbox('button').click(function() {
			tagAudioListDatagridReload();
		});

		tagAudioList_AudioTitleQue = $('#tagAudioList_AudioTitleQue').textbox({
			buttonText : '音频标题搜索',
			buttonIcon : 'icon-search',
			buttonAlign : 'right'
		});

		tagAudioList_AudioTitleQue.textbox('button').click(function() {
			tagAudioListDatagridReload();
		});

		tagAudioListDatagrid = $('#tagAudioList')
				.datagrid(
						{
							url : '${baseUrl}/tag/item/list',
							pagination : true,
							sortName : 'sort,updateTime',
							sortOrder : 'desc,desc',
							multiSort : true,
							//remoteSort : false,
							// 			rownumbers : true,
							queryParams : {
								itemType : 2
							},
							singleSelect : true,
							idField : 'id',
							striped : true,
							pageSize : 20,
							fitColumns : true,
							toolbar : '#tagAudioListToolBars',
							onLoadSuccess : function() {
								tagAudioSelectIndex = undefined;
								tagAudioSelectRow = undefined;
								tagAudioEditIndex = undefined;
								tagAudioEditRow = undefined;
							},
							onBeforeSelect : function(index, row) {
								if (tagAudioEditIndex != undefined) {
									return false;
								} else {
									tagAudioSelectIndex = index;
									tagAudioSelectRow = row;
								}
							},
							onBeforeEdit : function(index, row) {
								if (tagAudioEditIndex == undefined) {
									tagAudioEditIndex = index;
									tagAudioEditRow = row;
								}
							},
							onEndEdit : function(index, row, changes) {
								var _ed = $(this).datagrid('getEditor', {
									index : index,
									field : 'statusCode'
								});
								row.statusMsg = $(_ed.target).combobox(
										'getText');
							},
							// 			nowrap : false,
							columns : [ [
									{
										field : 'cover_url',
										title : '音频封面',
										width : 100,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.item) {
												var img = '<img src="'
														+ row.item.coverUrl
														+ '" style="width:103%;height:100px;"/>';
												return img;
											} else {
												return value;
											}
										}
									},
									{
										field : 'id',
										title : 'ID',
										align : 'center',
										width : 60
									},
									{
										field : 'title',
										title : '音频标题',
										width : 249,
										formatter : function(value, row, index) {
											if (row.item) {
												return row.item.title;
											} else {
												return value;
											}
										}
									},
									{
										field : 'tag',
										title : '标签',
										sortable : true,
										align : 'center',
										width : 200
									},
									{
										field : 'statusCode',
										title : '状态',
										width : 80,
										align : 'center',
										formatter : function(value, row, index) {
											var statusCodeSpan = '<span style="';
											if (row.statusCode == 1) {
												statusCodeSpan += 'color:#6B8E23;font-size:13px;font-weight:600;';
											} else if (row.statusCode == 2) {
												statusCodeSpan += 'color:#8B4726;font-size:13px;font-weight:600;';
											} else if (row.statusCode == 3) {
												statusCodeSpan += 'color:#104E8B;font-size:13px;font-weight:600;';
											}
											return statusCodeSpan += '">'
													+ row.statusMsg + '</span>';
										},
										editor : {
											type : 'combobox',
											options : {
												valueField : 'statusCode',
												textField : 'statusMsg',
												data : [ {
													statusCode : 1,
													statusMsg : '已发布'
												}, {
													statusCode : 2,
													statusMsg : '已下线'
												}, {
													statusCode : 3,
													statusMsg : '待发布'
												} ],
												required : true
											}
										}
									},
									{
										field : 'sort',
										title : '排序',
										width : 80,
										align : 'center',
										sortable : true,
										editor : {
											type : 'numberbox',
											options : {
												min : 0,
												precision : 2,
												required : true
											}
										}
									},
									{
										field : 'createTime',
										title : '创建时间',
										width : 100,
										align : 'left'
									},
									{
										field : 'updateTime',
										title : '更新时间',
										width : 100,
										align : 'left',
										sortable : true
									},
									{
										field : 'createUser',
										title : '创建者',
										width : 60,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.createUser) {
												return row.createUser.nickname;
											} else {
												return value;
											}
										}
									},
									{
										field : 'itemId',
										title : '音频ID',
										width : 80,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.item) {
												return row.item.id;
											} else {
												return value;
											}
										}
									},
									{
										field : 'entity',
										title : '音频实体',
										width : 80,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.item) {
												return row.item.entity;
											} else {
												return value;
											}
										}
									},
									{
										field : 'parent',
										title : '所属音频集合',
										width : 200,
										align : 'left',
										formatter : function(value, row, index) {
											if (row.item.parent) {
												return "<span>集合ID:"
														+ row.item.parent.id
														+ "</span><br/><span>集合标题:"
														+ row.item.parent.title
														+ "</span>";
											} else {
												return value;
											}
										}
									},
									{
										field : 'summary',
										title : '简介',
										width : 70,
										hidden : true,
										align : 'left',
										formatter : function(value, row, index) {
											if (row.item) {
												return row.item.summary;
											} else {
												return value;
											}
										}
									},
									{
										field : 'info',
										title : '详细介绍',
										hidden : true,
										width : 70,
										align : 'left',
										formatter : function(value, row, index) {
											if (row.item) {
												return row.item.info;
											} else {
												return value;
											}
										}
									},
									{
										field : 'playtime',
										title : '播放时间(秒)',
										width : 100,
										align : 'center',
										formatter : function(value, row, index) {
											if (row.item) {
												return row.item.playtime;
											} else {
												return value;
											}
										}
									},
									{
										field : 'itemStatusCode',
										title : '音频状态',
										width : 80,
										align : 'center',
										formatter : function(value, row, index) {
											var statusCodeSpan = '<span style="';
											if (row.item.statusCode == 1) {
												statusCodeSpan += 'color:#6B8E23;font-size:13px;font-weight:600;';
											} else if (row.item.statusCode == 2) {
												statusCodeSpan += 'color:#8B4726;font-size:13px;font-weight:600;';
											} else if (row.item.statusCode == 3) {
												statusCodeSpan += 'color:#104E8B;font-size:13px;font-weight:600;';
											}
											return statusCodeSpan += '">'
													+ row.item.statusMsg
													+ '</span>';
										}
									},
									{
										field : 'progressStatusCode',
										title : '音频进度',
										width : 100,
										align : 'center',
										formatter : function(value, row, index) {
											var statusCodeSpan = '<span style="';
											if (row.progressStatusCode == 2) {
												statusCodeSpan += 'color:#6B8E23;font-size:13px;font-weight:600;';
											} else if (row.progressStatusCode == 1) {
												statusCodeSpan += 'color:#FFEC8B;font-size:13px;font-weight:600;';
											}
											return statusCodeSpan += '">'
													+ row.progressStatusMsg
													+ '</span>';
										}
									} ] ],
							onDblClickRow : function(index, row) {
								$.ajax({
									data : {
										id : row.item.id
									},
									async : false,
									url : baseUrl + '/audio/info.html',
									success : function(html) {
										$('body').append(html);
									}
								});

							}
						});

		tagAudioListDatagrid.datagrid('getPager').pagination(
				{
					buttons : [ {
						iconCls : 'fa fa-2x fa-location-arrow',
						handler : function() {
							tagAudioListDatagrid.datagrid('gotoPage',
									tagAudioListDatagrid.datagrid('getPager')
											.find('.pagination-num').val());
						}
					} ]
				});

	});

	function tagAudioList_append() {

		$.ajax({
			url : baseUrl + '/tag/item/add.html?itemType=2',
			success : function(html) {
				$('body').append(html);
			}
		});
	}
	function tagAudioList_edit() {

		var tagAudioSelect = tagAudioListDatagrid.datagrid('getSelected');
		if (tagAudioEditIndex != undefined
				&& tagAudioEditIndex != tagAudioSelectIndex) {
			$.messager.alert('提示', '请完成已开启编辑行', 'warning');
			return;
		}

		if (tagAudioSelect) {
			if (tagAudioSelectIndex == undefined) {
				tagAudioSelectIndex = tagAudioListDatagrid.datagrid(
						"getRowIndex", tagAudioSelect);
			}

			tagAudioListDatagrid.datagrid('beginEdit', tagAudioSelectIndex);
			tagAudioSelectRow = tagAudioSelect;
		}
	}

	function tagAudioList_undo() {
		if (tagAudioSelectIndex != undefined) {
			tagAudioListDatagrid.datagrid('rejectChanges');
			tagAudioSelectIndex = undefined;
			tagAudioSelectRow = undefined;
			tagAudioEditIndex = undefined;
			tagAudioEditRow = undefined;
		}
	}

	function tagAudioList_accept() {
		if (tagAudioSelectIndex != undefined) {
			if (tagAudioListDatagrid.datagrid('validateRow',
					tagAudioSelectIndex)) {

				//验证发布状态
				var _edStatusCode = tagAudioListDatagrid.datagrid('getEditor',
						{
							index : tagAudioSelectIndex,
							field : 'statusCode'
						});
				var _statusMsg = $(_edStatusCode.target).combobox('getText');
				if (_statusMsg != '已发布' && _statusMsg != '待发布'
						&& _statusMsg != '已下线') {
					$.messager.alert('提示', '未知状态', 'warning');
					return;
				}

				//获取排序
				var _edSort = tagAudioListDatagrid.datagrid('getEditor', {
					index : tagAudioSelectIndex,
					field : 'sort'
				});

				var _Sort = $(_edSort.target).val();

				var _Data = {
					id : tagAudioEditRow.id,
					sort : _Sort,
					status : $(_edStatusCode.target).combobox('getValue')
				};

				$.ajax({
					data : _Data,
					async : false,
					type : 'POST',
					url : baseUrl + '/tag/item/update',
					success : function(data) {
						if (data.responseCode == '000') {
							tagAudioListDatagrid.datagrid('endEdit',
									tagAudioSelectIndex);

							tagAudioListDatagrid.datagrid('acceptChanges');

							tagAudioSelectIndex = undefined;
							tagAudioSelectRow = undefined;
							tagAudioEditIndex = undefined;
							tagAudioEditRow = undefined;

							$.messager.alert('提示', '修改成功', 'info');
						} else {
							$.messager.alert('提示', data.responseMessage,
									'error');
						}
					}
				});

			}
		}
	}

	function tagAudioList_tagListPage() {
		var tagAudioSelect = tagAudioListDatagrid.datagrid('getSelected');
		if (tagAudioSelect) {
			$.ajax({
				data : {
					itemId : tagAudioSelect.item.id,
					itemType : 2
				},
				async : false,
				url : baseUrl + '/tag/set.html',
				success : function(html) {
					$('body').append(html);
				}
			});
		}
	}

	function tagAudioList_remove() {
		var tagAudioSelect = tagAudioListDatagrid.datagrid('getSelected');
		if (tagAudioSelect) {
			$.messager
					.confirm(
							'确认',
							'您确认想要删除吗?',
							function(r) {
								if (r) {
									$
											.ajax({
												data : {
													id : tagAudioSelect.id
												},
												async : false,
												url : baseUrl
														+ '/tag/item/delete',
												success : function(data) {
													if (data.responseCode == '000') {
														$.messager
																.alert(
																		'提示',
																		"删除成功!",
																		'info');

														tagAudioListDatagrid
																.datagrid(
																		'deleteRow',
																		tagAudioListDatagrid
																				.datagrid(
																						'getRowIndex',
																						tagAudioSelect));

													} else {
														$.messager
																.alert(
																		'提示',
																		data.responseMessage,
																		'error');
													}
												}
											});
								}
							});
		}
	}
</script>
